import axios from 'axios'
// 方法装饰器

// 微笑
const sayNameDecorator = (times: number) => {
  return (target: any, name: string) => {
    for (let i = 0; i < times; i++) {
      console.log("微笑");
    }
  };
};

// 捕获异常
const catchDecorator: MethodDecorator = (
  target: any,
  name: string | symbol,
  desc: PropertyDescriptor
) => {
  const fn = desc.value;

  desc.value = async function (...args: any) {
    try {
      await fn.apply(this, args);
    } catch {
      console.log("发生了意外", name, "方法发生了意外");
			// await fn.apply(this, args);
    }
  };
};

class Greet {
  // @sayNameDecorator(2)
  sayHello() {
    console.log("你好， 欢迎！！！");
  }

  // @sayNameDecorator(10)
  order() {
    console.log("点餐");
  }

  muisc(song: string) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        Math.random() > 0.5 ? resolve(song) : reject();
      }, 2000);
    });
  }


  async sing(song: string) {
    const resp = await this.muisc(song);
    console.log("开始播放歌曲！！！！" + song);
  }
  @catchDecorator
	async fetchData() {
		const resp = await axios.get('/api/list')
	}
}

const g = new Greet();

// g.sing("我还想再活五百年！！！");



g.fetchData()
